﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.Products;
using MediatR;
using Product.API.ISerivces.Products;
using Product.API.Write.Applications.Commands.Products;

namespace Product.API.Write.Applications.CommandHandlers.Products
{
    /// <summary>
    /// 商品修改命令处理
    /// </summary>
    public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand, ApiResult<int>>
    {
        /// <summary>
        /// 产品方法
        /// </summary>
        private readonly IProductSerivces productSerivces;

        /// <summary>
        /// 轮廓映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productSerivces">产品方法</param>
        /// <param name="mapper">轮廓映射</param>
        public UpdateProductCommandHandler(IProductSerivces productSerivces, IMapper mapper)
        {
            this.productSerivces = productSerivces;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = EnumCode.Ok;
            result.Message = EnumCode.Ok.ToString();

            var data = mapper.Map<ERP.Domain.Products.Product>(request);
            int entity = await productSerivces.UpdateProduct(data);
            result.Data = entity;

            return await Task.FromResult(result);
        }
    }
}
